package com.tanhua.dubbo.api.mongo;

import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.mongo.Visitors;
import com.tanhua.domain.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserLikeApiImpl implements UserLikeApi  {

    @Autowired
    private MongoTemplate mongoTemplate;

    //查询好友数量
    public Long queryEachLikeCount(Long userId) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );
        return mongoTemplate.count(query, Friend.class);
    }

    //查询此用户喜欢的用户数量
    public Long queryLikeCount(Long userId) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );
        return mongoTemplate.count(query, UserLike.class);
    }

    //查询喜欢此用户的用户量
    public Long queryFanCount(Long userId) {
        Query query = new Query(
                Criteria.where("likeUserId").is(userId)
        );
        return mongoTemplate.count(query, UserLike.class);
    }

    //相互关注：好友
    public PageResult queryEachLikeList(Integer page, Integer pagesize, Long userId) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
        ).limit(pagesize).skip((page -1) * pagesize);

        List<Friend> list = mongoTemplate.find(query, Friend.class);
        List<RecommendUser> users = new ArrayList<>();

        for (Friend friend : list) {
            RecommendUser user = queryScore(friend.getFriendId(),friend.getUserId());
            users.add(user);
        }

        long counts  = mongoTemplate.count(query, Friend.class);
        return new PageResult(page,pagesize,(int) counts,users);
    }

    //我喜欢的用户列表
    public PageResult queryLikeList(Integer page, Integer pagesize, Long userId) {
        Query query = Query.query(
                Criteria.where("userId").is(userId))
                .limit(pagesize)
                .skip((page - 1) * pagesize);
        long count = this.mongoTemplate.count(query, UserLike.class);
        List<UserLike> list = this.mongoTemplate.find(query, UserLike.class);
        List<RecommendUser> users = new ArrayList<>();
        for (UserLike userLike : list) {
            users.add(queryScore(userLike.getLikeUserId(),userLike.getUserId()));
        }
        return new PageResult(page, pagesize, (int) count,users);
    }


    //查询喜欢我的
    public PageResult queryFanList(Integer page, Integer pageSize, Long userId) {
        Query query = Query.query(
                Criteria.where("likeUserId").is(userId))
                .limit(pageSize)
                .skip((page - 1) * pageSize);
        long count = this.mongoTemplate.count(query, UserLike.class);
        List<UserLike> list = this.mongoTemplate.find(query, UserLike.class);

        List<RecommendUser> users = new ArrayList<>();
        for (UserLike userLike : list) {
            users.add(queryScore(userLike.getUserId(),userLike.getLikeUserId()));
        }
        return new PageResult(page, pageSize, (int) count,users);
    }

    //查询访问记录
    public PageResult queryVisitorList(Integer page, Integer pageSize, Long userId) {
        Query query = Query.query(
                Criteria.where("userId").is(userId))
                .limit(pageSize)
                .skip((page - 1) * pageSize);
        List<Visitors> list = mongoTemplate.find(query, Visitors.class);
        long count = this.mongoTemplate.count(query, Visitors.class);

        List<RecommendUser> users = new ArrayList<>();
        for (Visitors visitors : list) {
            users.add(queryScore(visitors.getVisitorUserId(),userId));
        }
        return new PageResult(page, pageSize, (int) count,users);
    }

    @Override
    public void delete(Long userId, Long likeUserId) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
                .and("likeUserId").is(likeUserId)
        );
        mongoTemplate.remove(query, UserLike.class);
    }

    private RecommendUser queryScore(Long userId, Long toUserId) {
        Query query = Query.query(Criteria.where("toUserId").is(toUserId).and("userId").is(userId));
        RecommendUser user = this.mongoTemplate.findOne(query, RecommendUser.class);
        if (user == null) {
            user = new RecommendUser();
            user.setUserId(userId);
            user.setToUserId(toUserId);
            user.setScore(95d);
        }
        return user;
    }
}
